package components;

import java.util.ArrayList;
import java.util.List;

import util.Vector2D;
import entities.Entity;

/**
 * 
 * <p>
 * This class is an implementation of {@link Vectorial}.<br />
 * By adding derived instances, it is possible to manage velocity and
 * acceleration of an {@link Entity}.
 * </p>
 * 
 * @author Sebastian
 * 
 */
public class VectorialDerivationComponent extends EntityComponent implements
		Vectorial {

	protected Vector2D vector;

	private List<VectorialDerivationComponent> derivations;

	public VectorialDerivationComponent(Entity e) {
		super(e);
		derivations = new ArrayList<VectorialDerivationComponent>();
	}

	@Override
	public void tick(float dt) {
		Vector2D sum = Vector2D.ZERO_VECTOR;
		for (VectorialDerivationComponent vc : derivations) {
			vc.tick(dt);
			sum.add(vc.getVector().scale(dt));
		}
		vector = vector.add(sum);
	}

	public void addDerivation(VectorialDerivationComponent vectorial) {
		derivations.add(vectorial);
	}

	public void removeDerivation(Vectorial vectorial) {
		derivations.remove(vectorial);
	}

	@Override
	public Vector2D getVector() {
		return vector;
	}

	@Override
	public void setVector(Vector2D v) {
		this.vector = vector;
	}

}
